    package com.edu.bhu.admin.controller.admin;

import java.util.List;

import com.edu.bhu.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.admin.domain.PreDetection;
import com.edu.bhu.admin.service.IPreDetectionService;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import com.edu.bhu.common.core.page.TableDataInfo;

/**
 * 核酸检测Controller
 * 
 * @author yutianyang
 * @date 2022-04-23
 */
@Controller
@RequestMapping("/admin/detection")
public class PreDetectionController extends BaseController
{
    private String prefix = "admin/detection";

    @Autowired
    private IPreDetectionService preDetectionService;

    @RequiresPermissions("admin:detection:view")
    @GetMapping()
    public String detection()
    {
        return prefix + "/detection";
    }

    @RequiresPermissions("admin:detection:view")
    @GetMapping("/myDetection")
    public String myDetection()
    {
        return prefix + "/myDetection";
    }

    /**
     * 查询核酸检测列表
     */
    @RequiresPermissions("admin:detection:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PreDetection preDetection)
    {
        startPage();
        List<PreDetection> list = preDetectionService.selectPreDetectionList(preDetection);
        return getDataTable(list);
    }

    /**
     * 查询我的核酸检测列表
     */
    @RequiresPermissions("admin:detection:list")
    @PostMapping("/myList")
    @ResponseBody
    public TableDataInfo myList(PreDetection preDetection)
    {
        preDetection.setUserId(ShiroUtils.getSysUser().getUserId());
        startPage();
        List<PreDetection> list = preDetectionService.selectPreDetectionList(preDetection);
        return getDataTable(list);
    }

    /**
     * 导出核酸检测列表
     */
    @RequiresPermissions("admin:detection:export")
    @Log(title = "核酸检测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PreDetection preDetection)
    {
        List<PreDetection> list = preDetectionService.selectPreDetectionList(preDetection);
        ExcelUtil<PreDetection> util = new ExcelUtil<PreDetection>(PreDetection.class);
        return util.exportExcel(list, "核酸检测数据");
    }

    /**
     * 新增核酸检测
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存核酸检测
     */
    @RequiresPermissions("admin:detection:add")
    @Log(title = "核酸检测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PreDetection preDetection)
    {
        preDetection.setUserId(ShiroUtils.getSysUser().getUserId());
        return toAjax(preDetectionService.insertPreDetection(preDetection));
    }

    /**
     * 修改核酸检测
     */
    @RequiresPermissions("admin:detection:edit")
    @GetMapping("/edit/{detectionId}")
    public String edit(@PathVariable("detectionId") Long detectionId, ModelMap mmap)
    {
        PreDetection preDetection = preDetectionService.selectPreDetectionByDetectionId(detectionId);
        mmap.put("preDetection", preDetection);
        return prefix + "/edit";
    }

    /**
     * 修改保存核酸检测
     */
    @RequiresPermissions("admin:detection:edit")
    @Log(title = "核酸检测", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PreDetection preDetection)
    {
        return toAjax(preDetectionService.updatePreDetection(preDetection));
    }

    /**
     * 删除核酸检测
     */
    @RequiresPermissions("admin:detection:remove")
    @Log(title = "核酸检测", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(preDetectionService.deletePreDetectionByDetectionIds(ids));
    }
}
